package com.hsy.linemoney.modules.lanuch;

import android.os.Bundle;
import android.os.Message;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.deparse.dpframe.ui.android.app.activity.DPBaseActivity;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.CurrencyService;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.apiservice.ServiceTypeService;
import com.hsy.linemoney.modules.common.CommonBaseActivity;
import com.hsy.linemoney.modules.common.LocalSetActivity;
import com.hsy.linemoney.modules.main.MainActivity;
import com.hsy.linemoney.utils.CommonUtils;

/**
 * Created by Administrator on 2016/7/5.
 */
public class SplashActivity extends CommonBaseActivity {
    private static final int duration = 3000;
    private long start = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        appInit();
    }

    private void appInit() {

    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        start = System.currentTimeMillis();
        if (isLocalSetted()) {
            new CurrencyService().getCurrency(currencyListResultCallback);
        } else {
            setLocal();
        }
    }

    private Callback<CurrencyService.CurrencyListResult> currencyListResultCallback = new Callback<CurrencyService.CurrencyListResult>() {
        @Override
        public void onSuccess(int what, CurrencyService.CurrencyListResult result) {
            new ServiceTypeService().getServiceTypeList(serviceTypeListResultCallback);
        }

        @Override
        public void onError(int what, int type, String msg, Exception e) {
            new ServiceTypeService().getServiceTypeList(serviceTypeListResultCallback);
        }
    };

    private Callback<ServiceTypeService.ServiceTypeListResult> serviceTypeListResultCallback = new Callback<ServiceTypeService.ServiceTypeListResult>() {
        @Override
        public void onSuccess(int what, ServiceTypeService.ServiceTypeListResult result) {
            pubJumpMsg();
        }

        @Override
        public void onError(int what, int type, String msg, Exception e) {
            pubJumpMsg();
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        removeMessage(0);
    }

    private boolean isLocalSetted() {
        return LineMoneyContext.isLocalSetted();
    }

    private void setLocal() {
        startActivity(LocalSetActivity.class);
    }

    private void pubJumpMsg() {
        long cost = System.currentTimeMillis() - start;
        removeMessage(0);
        if (cost < duration) {
            sendEmptyMessageDelayed(0, duration - cost);
        } else {
            sendEmptyMessage(0);
        }
    }

    @Override
    protected void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (CommonUtils.getIsFirstInUse(this)) {
            DPLogUtil.d("Splash->Launcher");
            startActivity(LauncherActivity.class);
        } else {
//            startActivity(LoginActivity.class);
            DPLogUtil.d("Splash->main");
            startActivity(MainActivity.class);
        }
        finish();
    }
}
